package com.ihisomp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ihisomp.base.Page;
import com.ihisomp.base.Result;
import com.ihisomp.dto.OmpRoleDto;
import com.ihisomp.dto.PathTreeDto;
import com.ihisomp.entity.OmpRole;
import com.ihisomp.entity.OmpRolePage;
import com.ihisomp.mapper.OmpRoleMapper;
import com.ihisomp.service.IOmpRolePageService;
import com.ihisomp.service.IOmpRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihisomp.service.ISysGetNoService;
import com.ihisomp.util.ComUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 标题: OmpRoleServiceImpl.java
 * 业务描述: 运营管理角色信息 服务实现类
 * 作者: zy
 * 日期: 2021/9/13
 * 版本: V1.0
 */
@Service
public class OmpRoleServiceImpl extends ServiceImpl<OmpRoleMapper, OmpRole> implements IOmpRoleService {

    @Autowired
    private ISysGetNoService sysGetNoService;

    @Autowired
    private IOmpRolePageService ompRolePageService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param req
     * @return
     */
    @Override
    public Result search(long page, long size, OmpRoleDto req) {
        //  查询条件
        QueryWrapper<OmpRole> query = new QueryWrapper<>();
        if (req != null) {
            if (StringUtils.isNotBlank(req.getRoleName())) {
                query.like("role_name", req.getRoleName());
            }
        }
        query.orderByAsc("seq_no");
        // 分页查询
        IPage<OmpRole> data = baseMapper.selectPage(new Page<>(page, size), query);
        return Result.ok(data);
    }

    /**
     * 新增方法
     *
     * @param ompRoleDto
     * @return
     */
    @Override
    public Result add(OmpRoleDto ompRoleDto) {
        if (ompRoleDto == null || StringUtils.isBlank(ompRoleDto.getRoleName())) {
            return Result.error("角色名称不能为空！");
        }
        OmpRole ompRole = new OmpRole();
        BeanUtils.copyProperties(ompRoleDto, ompRole);
        ompRole.setRoleKey(sysGetNoService.getSysNo("t_omp_role"));
        if (baseMapper.insert(ompRole) > 0) {
            return Result.ok();
        }
        return Result.error("新增角色失败！");
    }

    /**
     * 删除方法
     *
     * @param roleKey
     * @return
     */
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    @Override
    public Result delete(String roleKey) {
        if (baseMapper.deleteById(roleKey) > 0) {
            if (deleteOmpRolePage(roleKey)) {
                return Result.ok();
            }
        }
        return Result.error("删除角色失败！");
    }

    /**
     * 更新方法
     *
     * @param roleKey
     * @param ompRoleDto
     * @return
     */
    @Override
    public Result update(String roleKey, OmpRoleDto ompRoleDto) {
        if (ompRoleDto.getRoleKey() == null) {
            ompRoleDto.setRoleKey(roleKey);
        }
        OmpRole ompRole = new OmpRole();
        BeanUtils.copyProperties(ompRoleDto, ompRole);
        if (baseMapper.updateById(ompRole) > 0) {
            return Result.ok();
        }
        return Result.error("更新角色失败！");
    }

    /**
     * 查询方法
     *
     * @param roleKey
     * @return
     */
    @Override
    public Result get(String roleKey) {
        OmpRoleDto ompRoleDto = new OmpRoleDto();
        BeanUtils.copyProperties(baseMapper.selectById(roleKey), ompRoleDto);
        ompRoleDto.setPathList(ompRolePageService.getPageByRoleKey(roleKey));
        return Result.ok(ompRoleDto);
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    @Override
    public Result selectList() {
        QueryWrapper<OmpRole> query = new QueryWrapper<>();
        List<OmpRole> roles = baseMapper.selectList(query);
        return Result.ok(roles);
    }

    /**
     * 更新角色权限
     *
     * @param roleKey
     * @param ompRoleDto
     * @return
     */
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    @Override
    public Result updateRolePath(String roleKey, OmpRoleDto ompRoleDto) {
        if (ompRoleDto.getPathList() == null || ompRoleDto.getPathList().size() == 0) {
            return Result.error("角色权限不能为空！");
        }
        if (!deleteOmpRolePage(roleKey)) {
            return Result.error("更新角色失败！");
        }
        if (!addOmpRolePage(roleKey, ompRoleDto.getPathList())) {
            return Result.error("更新角色失败！");
        }
        return Result.ok();
    }

    /**
     * 批量新增角色程序路径
     *
     * @param roleKey
     * @param pathList
     * @return
     */
    private boolean addOmpRolePage(String roleKey, List<PathTreeDto> pathList) {
        OmpRolePage ompRolePage = new OmpRolePage();
        ompRolePage.setRoleKey(roleKey);
        for (int i = 0; i < pathList.size(); i++) {
            if ("ROOT".equals(pathList.get(i).getId())) {
                continue;
            }
            ompRolePage.setRolePageKey(ComUtil.createPrimaryKey());
            ompRolePage.setPagePathKey(pathList.get(i).getId());
            if (!ompRolePageService.save(ompRolePage)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 批量删除角色程序路径
     *
     * @param roleKey
     * @return
     */
    private boolean deleteOmpRolePage(String roleKey) {
        if (ompRolePageService.deleteByRoleKey(roleKey) > -1) {
            return true;
        }
        return false;
    }
}
